package com.qiqu.service.impl;

import com.qiqu.dao.ArticleDao;
import com.qiqu.dao.CommentsDao;
import com.qiqu.entity.Article;
import com.qiqu.entity.Comments;
import com.qiqu.entity.UsersReport;
import com.qiqu.dao.UsersReportDao;
import com.qiqu.service.ArticleService;
import com.qiqu.service.CommentsService;
import com.qiqu.service.UsersReportService;
import com.qiqu.utils.JsonUtils;
import org.n3r.idworker.Sid;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service("usersReportService")
public class UsersReportServiceImpl implements UsersReportService {
    @Resource
    private UsersReportDao usersReportDao;
    @Resource
    private ArticleDao articleDao;
    @Resource
    private CommentsDao commentsDao;
    @Resource
    private Sid sid;
    @Resource
    private ArticleService articleService;
    @Resource
    private CommentsService commentsService;

    @Override
    public boolean addReport(UsersReport report) {
        report.setId(sid.nextShort());
        report.setCreateTime(new Date());
        return usersReportDao.insert(report)>0;
    }

    @Override
    public boolean deleteReport(String id) {
        return usersReportDao.deleteReport(id)>0;
    }

    @Override
    public boolean deleteArticle(String id) {
        // 获取文章id
        UsersReport report = usersReportDao.queryById(id);
        String articleId = report.getDealArticleId();
        // 删除评论
        int i = commentsDao.deleteByArticleId(articleId);
        System.out.println(i);
        if (i>0) {
            // 删除文章
            boolean flag1 = articleService.deleteById(articleId);
            if (flag1) {
                // 删除举报信息
                return usersReportDao.deleteReport(id)>0;
            }
        }
        return false;
    }

    @Override
    public List<UsersReport> getAllReport() {
        return usersReportDao.selectAll();
    }

    @Override
    public UsersReport queryById(String id) {
        System.out.println(usersReportDao.queryById(id));
        return usersReportDao.queryById(id);
    }
}